package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.HzyLocation;
import com.bdqn.tour.service.HzyLocationService;
import com.bdqn.tour.service.impl.HzyLocationServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/updateLocation")
public class UpdateLocationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        //获取参数
        String receiverName = req.getParameter("receiverName");
        String mobilePhone = req.getParameter("mobilePhone");
        String areaCode = req.getParameter("areaCode");
        String phoneNumber = req.getParameter("phoneNumber");
        String extension = req.getParameter("extension");
        String province = req.getParameter("province");
        String city = req.getParameter("city");
        String district = req.getParameter("district");
        String streetAddress = req.getParameter("streetAddress");
        String postalCode = req.getParameter("postalCode");
        int id = Integer.parseInt(req.getParameter("id"));

        HzyLocation location = new HzyLocation();
        location.setId(id);
        location.setReceiverName(receiverName);
        location.setMobilePhone(mobilePhone);
        location.setAreaCode(areaCode);
        location.setPhoneNumber(phoneNumber);
        location.setExtension(extension);
        location.setProvince(province);
        location.setCity(city);
        location.setDistrict(district);
        location.setStreetAddress(streetAddress);
        location.setPostalCode(postalCode);

        HzyLocationService locationService = new HzyLocationServiceImpl();
        boolean flag = locationService.updateLocation(location);

        Map<String, Object> map = new HashMap<>();
        map.put("success", flag);
        map.put("message", flag ? "修改成功" : "修改失败");

        String json = JSON.toJSONString(map);
        resp.getWriter().print(json);
    }
}
